package cn.ac.zing.server.hj212.test;

import cn.ac.zing.server.util.CRC16Util;
import cn.ac.zing.server.util.DateUtil;
import cn.ac.zing.server.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Random;

/**
 * @Author：dcy
 * @name：LoopSend
 * @Date：2024/5/21 16:43
 */
public class LoopSend extends Thread {
    JSONObject config = null;

    public LoopSend(JSONObject config) {
        this.config = config;
    }

    @Override
    public void run() {
        try {
            String cn = config.getString("cn");

            switch (cn) {
                case "2011":
                    while (true) {
                        loop();
                        Thread.sleep(180 * 1000);
                    }
                case "2051":
                    while (true) {
                        loop();
                        Thread.sleep(600 * 1000);
                    }
                case "2061":
                    while (true) {
                        loop();
                        Thread.sleep(3600 * 1000);
                    }
                case "2031":
                    while (true) {
                        loop();
                        Thread.sleep(86400 * 1000);
                    }
            }
        } catch (Exception e) {

        }
    }

    private void loop() throws Exception {
        String ip = config.getString("ip");
        int port = config.getInteger("port");
        String mn = config.getString("mn");
        String cn = config.getString("cn");
        JSONObject factors = config.getJSONObject("factors");

        StringBuilder str = new StringBuilder();
        str.append("QN=").append(DateUtil.getCurrentDate(DateUtil.DF_SALL17)).append(";");
        str.append("ST=22;");
        str.append("CN=").append(cn).append(";");
        str.append("PW=123456;");
        str.append("MN=").append(mn).append(";");
        str.append("Flag=7;");
        str.append("CP=&&");
        if (cn.equals("2011")) {
            str.append("DataTime=").append(DateUtil.getCurrentTime().replaceAll("\\D", "")).append(";");
        } else if (cn.equals("2051")) {
            str.append("DataTime=").append((DateUtil.getCurrentDate(DateUtil.DF_SEC19).substring(0, 15) + "0:00").replaceAll("\\D", "")).append(";");
        } else if (cn.equals("2061")) {
            str.append("DataTime=").append(DateUtil.getCurrentDate(DateUtil.DF_HOUR19).replaceAll("\\D", "")).append(";");
        } else if (cn.equals("2031")) {
            str.append("DataTime=").append(DateUtil.getCurrentDate(DateUtil.DF_DAY19).replaceAll("\\D", "")).append(";");
        }

        JSONObject factor = new JSONObject();

        Random random = new Random();
        double value = 0d;
        int min = 0, max = 0, scale = 0;
        for (String key : factors.keySet()) {
            factor = factors.getJSONObject(key);
            min = factor.getInteger("min");
            max = factor.getInteger("max");
            scale = factor.getInteger("scale");

            value = new BigDecimal(random.nextDouble() * min + (max - min)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
            if (cn.equals("2011")) {
                str.append(key).append("-Rtd=").append(value).append(",");
                str.append(key).append("-Flag=N;");
            } else {
                str.append(key).append("-Avg=").append(value).append(",");
                str.append(key).append("-Min=").append(new BigDecimal(random.nextDouble() * min + (value - min)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue()).append(",");
                str.append(key).append("-Max=").append(new BigDecimal(random.nextDouble() * value + (max - value)).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue()).append(",");
                str.append(key).append("-Flag=N;");
            }
        }
        str.append("&&");
        str = new StringBuilder("##" + StringUtil.getLength(str.toString()) + str + CRC16Util.calcCrc16(str.toString()) + "\r\n");

        SocketClient.send(ip, port, str.toString());
    }
}
